<template>
	<view>
		<view class="title">
			最多可选2项
		</view>
		<view class="tipflex">
			<view class="item" v-for="(item,index) in list" :key="index" :class="item.default?'itemact':''"
				@click="changeAll(item)">
				{{item.code}}
			</view>
		</view>
		<view class="footerbtn" @click="handleBc">
			<view class="cont">
				<view class="btn">
					保存
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		productCategory
	} from '@/common/http.api.js';
	export default {
		data() {
			return {
				list: [],
				selectList: []
			};
		},
		onLoad(query) {
			console.log(query);
			this.selectList = query.nowtype ? query.nowtype.split(',') : []
			productCategory({}).then(res => {
				res.data.forEach(item => {
					item.default = this.selectList.indexOf(item.code) >= 0
				})
				this.list = res.data
			})
			console.log(this.selectList);
		},
		methods: {
			handleBc() {
				if (this.selectList.length == 0) {
					uni.showToast({
						title: '请选择一个品类',
						icon: 'none'
					})
				}
				uni.$emit('nowType', this.selectList)
				uni.navigateBack()
			},
			changeAll(item) {
				item.default = !item.default
				let index = this.selectList.indexOf(item.code)
				let select = index >= 0
				if (select) {
					this.selectList.splice(index, 1)
				} else {
					if (this.selectList.length >= 2) {
						this.list.find(obj =>obj.code ==  this.selectList[0]).default = false
						this.selectList.splice(0, 1)
					}
					this.selectList.push(item.code)
				}
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #F5F5F5;
	}

	.title {
		padding: 20rpx 0rpx 0rpx 20rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		font-size: 30rpx;
		color: #979797;
	}

	.tipflex {
		display: flex;
		flex-wrap: wrap;
		width: 100%;
		padding: 20rpx;
		box-sizing: border-box;

		.item {
			height: 76rpx;
			background: #FFFFFF;
			border-radius: 8rpx 8rpx 8rpx 8rpx;
			padding: 0rpx 20rpx;
			line-height: 76rpx;
			margin-right: 20rpx;
			margin-bottom: 20rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 28rpx;
			color: #000000;
		}

		.itemact {
			background: #000000 !important;
			color: #FFFFFF !important;
		}
	}

	.footerbtn {
		width: 750rpx;
		height: 120rpx;
		background: #FFFFFF;
		position: fixed;
		bottom: 0%;
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);

		.cont {
			display: flex;
			padding: 10rpx 20rpx;
			height: 120rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 32rpx;
			align-items: center;
			justify-content: center;

			.btn {
				width: 670rpx;
				height: 100rpx;
				background: #000000;
				border-radius: 8rpx 8rpx 8rpx 8rpx;
				color: #FFFFFF;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 32rpx;
				color: #FFFFFF;
				line-height: 100rpx;
				text-align: center;
			}

			.absbtn {
				background: #808080;
			}
		}
	}
</style>